const { query } = require("./mysql");
const {
	YD_datetimeToString,
	YD_whereSql,
	YD_setSql,
} = require("lib/utils/common");

/** @name 批量插入车位 **/
exports.yd_batchInsert = (options) => {
	const sql = `insert into ch_place (code, type, number, createTime, updateTime) values ?`;
	return query(sql, [options]);
};

/** @name 车位列表 **/
exports.yd_list = (options) => {
	const sql = `select
						yp.id, yp.code, yp.idle, yp.type, yp.number, yp.images, yp.note, yp.userId,
						yu.realname, yu.phone,
						${YD_datetimeToString("yp.createTime")},
						${YD_datetimeToString("yp.updateTime")}
						from ch_place yp
						left join ch_user yu on yp.userId=yu.uuid
						${YD_whereSql([
							{ field: "code", type: "模糊", value: options.code },
							{ field: "idle", type: "全等", value: options.idle },
							{ field: "type", type: "全等", value: options.type },
							{ field: "number", type: "模糊", value: options.number },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
						])}
						order by yp.id desc
						limit ${(options.pageNum - 1) * options.pageSize},${options.pageSize}
            `;
	return query(sql);
};

/** @name 车位总量 **/
exports.yd_count = (options) => {
	const sql = `select count(*) as count
						from ch_place yp
						left join ch_user yu on yp.userId=yu.uuid
						${YD_whereSql([
							{ field: "code", type: "模糊", value: options.code },
							{ field: "idle", type: "全等", value: options.idle },
							{ field: "type", type: "全等", value: options.type },
							{ field: "number", type: "模糊", value: options.number },
							{ field: "realname", type: "模糊", value: options.realname },
						])}
            `;
	return query(sql);
};

/** @name 添加车位 **/
exports.yd_add = (options) => {
	const sql = `insert into ch_place set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 编辑车位 **/
exports.yd_update = (options, condition) => {
	const sql = `update ch_place set ${YD_setSql(options)} where id=${
		condition.id
	}`;
	console.log("sql", sql);
	return query(sql);
};

/** @name 删除车位 **/
exports.yd_delete = (id) => {
	const sql = `delete from ch_place where id='${id}'`;
	return query(sql);
};

/** @name 简易车位列表 **/
exports.yd_easyList = () => {
	const sql = `select
						id, code, type, number, userId, idle
						from ch_place
						order by type desc, id
            `;
	return query(sql);
};
